package com.zhou.controller;

import com.zhou.myAnnotation.ServiceHandleType;
import com.zhou.service.ServiceHandle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.ServiceLoader;

@RestController
@RequestMapping("handle")
@Api("ServiceLoader测试")
public class ServiceHandleTest {


    @GetMapping("serviceLoaderTest")
    @ApiOperation("serviceLoaderTest")
    public void serviceLoaderTest(){
        ServiceLoader<ServiceHandle> serviceHandles = ServiceLoader.load(ServiceHandle.class);
        for (ServiceHandle serviceHandle : serviceHandles) {
            serviceHandle.testMethod();
        }
    }

    public static void main(String[] args) {
        ServiceLoader<ServiceHandle> serviceHandles = ServiceLoader.load(ServiceHandle.class);
        for (ServiceHandle serviceHandle : serviceHandles) {
            try {
                Method method = serviceHandle.getClass().getMethod("testMethod");
                ServiceHandleType annotation = method.getAnnotation(ServiceHandleType.class);
                if(Arrays.asList(annotation.value()).contains("prod")){
                    serviceHandle.testMethod();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
